// pages/tweet/publish/topic/topic.js
import {
  TOPIC_RECOMMEND,
} from "../../../../request/api";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    topic: '',
    topicList: [],
    localList: [],
    userId: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.userId = getApp().globalData.userId;
    this.loadRecommendTopic();
    this.loadLocalTopic();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  confirmTopic: function(){
    if(this.data.topic.length == 0){
      wx.navigateBack();
      return;
    }

    var pages = getCurrentPages();
    var prePage = pages[pages.length - 2];
    prePage.setData({
      topicTitle: this.data.topic,
      reloadByTopic: true,
    });
    wx.navigateBack();
  },

  inputChange: function (e) {
    var key = e.detail.value;
    this.setData({
      topic: key,
    });
  },

  itemClick: function (e) {
    var title = e.currentTarget.dataset.title;
    this.data.topic = title;
    this.confirmTopic();
  },

  loadRecommendTopic: function () {
    TOPIC_RECOMMEND().then(res => {
      this.loadingFollows = false;
      if (res.data.code == 1) {
        this.setData({
          topicList: res.data.result,
        });
      }
    });
  },

  loadLocalTopic: function(){

  }
})